﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AviumX.AviumCore.WinAPI
{
    class Window
    {
        //Define the windows width and height
        public int width = 0;
        public int height = 0;
        //Define it's location
        public AviumCore.Screen.Graphics.Vector location = new AviumCore.Screen.Graphics.Vector();
        //Admin Privlages:
        public bool Admin = false;
        //Define it's name and text
        public string name = "";
        public string text = "";
        //Define it's signiture, used for the market and other things
        public string signutre = "";
        //Is the window running? By defualt yes.
        public bool Runing = true;
        //The windows task, this is the code runner that is tied into this window.
        
        //Waiting For IntelloTech To Write Up A Language.
        //Toxickill will implement code interpreter.


        //Have we started the task yet?
        public bool StartedTask = false;
        //Is the window in front?
        public bool infront = false;
        //Move handlers
        public bool clicked = false;
        public int savedx = 0;
        public int savedy = 0;
        public bool inclick = false;
        //Window style
        public enum WindowStyle
        {
            Default,
            Aero,
            Metro
        };
        //Current style
        public WindowStyle CurrentStyle = WindowStyle.Default;
        //These are the new window handlers.
        #region "New Window Handlers"
        public Window(uint x, uint y, uint width, uint height, string code)
        {
            this.location.x = (int)x;
            this.location.y = (int)y;
            this.width = (int)width;
            this.height = (int)height;
            //Load the code into the windowtask.
            //this.WindowTask.LoadCode(code);

            //Also Intellotechs Job :)
        }
        public Window(uint x, uint y, uint width, uint height)
        {
            this.location.x = (int)x;
            this.location.y = (int)y;
            this.width = (int)width;
            this.height = (int)height;
        }
        public Window(int x, int y, int width, int height, string code)
        {
            this.location.x = x;
            this.location.y = y;
            this.width = width;
            this.height = height;
            //this.WindowTask.LoadCode(code);
            //Intellotech once again
        }
        public Window(int x, int y, int width, int height)
        {
            this.location.x = x;
            this.location.y = y;
            this.width = width;
            this.height = height;
        }
        public Window(AviumCore.Screen.Graphics.Vector newloc, int width, int height, string code)
        {
            this.location = newloc;
            this.width = width;
            this.height = height;
            //this.WindowTask.LoadCode(code);
            //IntelloTech
        }
        public Window(AviumCore.Screen.Graphics.Vector newloc, int width, int height)
        {
            this.location = newloc;
            this.width = width;
            this.height = height;
        }
        Window()
        {
            //Nothing Yet...
        }
        #endregion
        #region "Window Handlers"
        public void ActivateWindow()
        {
            //This activates the window. // Toxickill - A window is a program.
            this.Runing = true;
        }
        public void Update()
        {
            //Taskmanager - @intellotech's Job.

            #region " Move event "
            if (inclick == false)
            {
                if (AviumX.AviumCore.Hardware.Mouse.Cursor.CheckMouseLeftClick(this.location.x, this.location.y, this.width - 16, 14) == true)
                {
                    if (clicked == false)
                    {
                        savedx = AviumX.AviumCore.Hardware.Mouse.Cursor.Mouse.Getx() - this.location.x;
                        savedy = AviumX.AviumCore.Hardware.Mouse.Cursor.Mouse.Gety() - this.location.y;
                        clicked = true;
                    }
                    inclick = true;

                }
            }
            else
            {

                if (AviumX.AviumCore.Hardware.Mouse.Cursor.CurrentState == AviumX.AviumCore.Hardware.Mouse.Cursor.MouseState.LeftClick)
                {
                    if (AviumX.AviumCore.Hardware.Mouse.Cursor.Mouse.Getx() - savedx < 1)
                    {
                        this.location.x = 0;
                    }
                    else if ((AviumX.AviumCore.Hardware.Mouse.Cursor.Mouse.Getx() - savedx) + width > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width)
                    {
                        this.location.x = AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - width;
                    }
                    else
                    {
                        this.location.x = AviumX.AviumCore.Hardware.Mouse.Cursor.Mouse.Getx() - savedx;
                    }
                    if (AviumX.AviumCore.Hardware.Mouse.Cursor.Mouse.Gety() - savedy < 1)
                    {
                        this.location.y = 0;
                    }
                    else if ((AviumX.AviumCore.Hardware.Mouse.Cursor.Mouse.Gety() - savedy) + height > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height)
                    {
                        this.location.y = AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - height;
                    }
                    else
                    {
                        this.location.y = AviumX.AviumCore.Hardware.Mouse.Cursor.Mouse.Gety() - savedy;
                    }
                }
                else
                {
                    inclick = false;
                    clicked = false;
                }
            }
            #endregion
            #region " Close Event "
            if (AviumX.AviumCore.Hardware.Mouse.Cursor.CheckMouseLeftClick(this.location.x + this.width - 12 - 1, this.location.y + 1, 12, 8) == true)
            {
                /*
                 * Window is now no longer running.
                 */
                this.Runing = false;
            }
            #endregion

        }
        #endregion
        #region "Draw Event"
        public void Draw()
        {
            #region " Code Update "
            //IntelloTech's Job Once Again.
            #endregion
            //Border
            AviumX.AviumCore.Screen.Graphics.Drawing.DrawRectangle(this.location.x, this.location.y, this.width, this.height, AviumX.AviumCore.Screen.ScreenX.SystemColors.Black);
            //Back Color
            AviumX.AviumCore.Screen.Graphics.Drawing.DrawRectangle(this.location.x + 1, this.location.y + 1, this.width - 2, this.height - 2, AviumX.AviumCore.Screen.ScreenX.SystemColors.White);
            //Top Bar, make it so that if its infront it bar is different.
            if (this.infront == true)
            {
                //Core.Graphics.Drawing.DrawRectangle(this.location.x + 1, this.location.y + 1, this.width - 2, 8, Screen.ScreenHandler.SystemColors.Gray);
                if (CurrentStyle == WindowStyle.Default)
                {
                    DrawTopBar(this.width - 2);
                }
                if (CurrentStyle == WindowStyle.Aero)
                {
                    DrawTopBar2(this.width - 2);
                }
                if (CurrentStyle == WindowStyle.Metro)
                {
                    DrawTopBar3(this.width - 2);
                }
            }
            else
            {
                //Core.Graphics.Drawing.DrawRectangle(this.location.x + 1, this.location.y + 1, this.width - 2, 8, Screen.ScreenHandler.SystemColors.LightGray);
                if (CurrentStyle == WindowStyle.Default)
                {
                    DrawTopBar(this.width - 2);
                }
                if (CurrentStyle == WindowStyle.Aero)
                {
                    DrawTopBar2(this.width - 2);
                }
                if (CurrentStyle == WindowStyle.Metro)
                {
                    DrawTopBar3(this.width - 2);
                }
            }
            //Close button
            //Core.Graphics.Drawing.DrawRectangle(this.location.x + this.width - 12 - 1, this.location.y + 1, 12, 8, Screen.ScreenHandler.SystemColors.Red);
            if (CurrentStyle == WindowStyle.Default)
            {
                DrawCloseButton();
            }
            if (CurrentStyle == WindowStyle.Aero)
            {
                DrawCloseButton2();
            }
            if (CurrentStyle == WindowStyle.Metro)
            {
                DrawCloseButton3();
            }
        }
        #endregion
        #region " Resources "
        #region " Top Bar "
        /*
         * Top bar pattern
         * White
         * White
         * Black
         * White
         * Black
         * White
         * Black
         * White
         * Black
         * White
         * Black
         * White
         * White
         * Black
         */

        //1x12
        public static uint White = (uint)AviumX.AviumCore.Screen.ScreenX.SystemColors.White;
        public static uint Black = (uint)AviumX.AviumCore.Screen.ScreenX.SystemColors.Black;
        public static uint Blue = (uint)AviumX.AviumCore.Screen.ScreenX.SystemColors.CornflowerBlue;
        public static uint Gold = (uint)AviumX.AviumCore.Screen.ScreenX.SystemColors.Gold;
        public static uint DarkGray = (uint)AviumX.AviumCore.Screen.ScreenX.SystemColors.DarkGray;
        public uint[] TopBarPannelPeace = { White,
                                            White,
                                            Black,
                                            White,
                                            Black,
                                            White,
                                            Black,
                                            White,
                                            Black,
                                            White,
                                            White,
                                            Black};

        public uint[] TopBarPannelPeace2 = {DarkGray,
                                            DarkGray,
                                            Black,
                                            DarkGray,
                                            Black,
                                            DarkGray,
                                            Black,
                                            DarkGray,
                                            Black,
                                            DarkGray,
                                            DarkGray,
                                            Black};

        public uint[] TopBarPannelPeace3 = {DarkGray,
                                            DarkGray,
                                            Black,
                                            DarkGray,
                                            Gold,
                                            DarkGray,
                                            Gold,
                                            DarkGray,
                                            Black,
                                            DarkGray,
                                            DarkGray,
                                            Black};

        public void DrawTopBar(int width)
        {
            for (int i = 0; i < width; i++)
            {
                AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBasedOffOfSystemColorsInBuffer(TopBarPannelPeace, 1, 12, this.location.x + 1 + i, this.location.y + 1);
            }
        }
        public void DrawTopBar2(int width)
        {
            for (int i = 0; i < width; i++)
            {
                AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBasedOffOfSystemColorsInBuffer(TopBarPannelPeace2, 1, 12, this.location.x + 1 + i, this.location.y + 1);
            }
        }
        public void DrawTopBar3(int width)
        {
            for (int i = 0; i < width; i++)
            {
                AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBasedOffOfSystemColorsInBuffer(TopBarPannelPeace3, 1, 12, this.location.x + 1 + i, this.location.y + 1);
            }
        }
        #endregion
        #region " Close button "
        //12x12
        public static uint W = (uint)AviumX.AviumCore.Screen.ScreenX.SystemColors.White;
        public static uint B = (uint)AviumX.AviumCore.Screen.ScreenX.SystemColors.Black;
        public static uint G = (uint)AviumX.AviumCore.Screen.ScreenX.SystemColors.DarkGray;
        public static uint R = (uint)AviumX.AviumCore.Screen.ScreenX.SystemColors.Red;

        public uint[] Button = {0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,W,W,W,W,W,W,W,0,0,0,
                                0,0,W,W,W,W,W,W,W,0,0,0,
                                0,0,W,W,B,W,B,W,W,0,0,0,
                                0,0,W,W,W,B,W,W,W,0,0,0,
                                0,0,W,W,B,W,B,W,W,0,0,0,
                                0,0,W,W,W,W,W,W,W,0,0,0,
                                0,0,W,W,W,W,W,W,W,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,};

        public uint[] Button2 = {0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,G,G,G,G,G,G,G,0,0,0,
                                0,0,G,G,G,G,G,G,G,0,0,0,
                                0,0,G,G,W,G,W,G,G,0,0,0,
                                0,0,G,G,G,W,G,G,G,0,0,0,
                                0,0,G,G,W,G,W,G,G,0,0,0,
                                0,0,G,G,G,G,G,G,G,0,0,0,
                                0,0,G,G,G,G,G,G,G,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,};

        public uint[] Button3 = {0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,G,G,G,G,G,G,G,0,0,0,
                                0,0,G,G,G,G,G,G,G,0,0,0,
                                0,0,G,G,R,G,R,G,G,0,0,0,
                                0,0,G,G,G,R,G,G,G,0,0,0,
                                0,0,G,G,R,G,R,G,G,0,0,0,
                                0,0,G,G,G,G,G,G,G,0,0,0,
                                0,0,G,G,G,G,G,G,G,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,};


        public void DrawCloseButton()
        {
            AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBasedOffOfSystemColorsInBuffer(Button, 12, 12, this.location.x + this.width - 12 - 1, this.location.y + 1);
        }
        public void DrawCloseButton2()
        {
            AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBasedOffOfSystemColorsInBuffer(Button2, 12, 12, this.location.x + this.width - 12 - 1, this.location.y + 1);
        }
        public void DrawCloseButton3()
        {
            AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBasedOffOfSystemColorsInBuffer(Button3, 12, 12, this.location.x + this.width - 12 - 1, this.location.y + 1);
        }
        #endregion
        #endregion
    }
}
